//
//  RegistViewController.m
//  fastwatch
//
//  Created by lanou3g on 15/10/31.
//  Copyright © 2015年 songhaowen. All rights reserved.
//

#import "RegistViewController.h"
#import "RegisterProtocolController.h"
#import "UserController.h"
#import <BmobSDK/Bmob.h>


@interface RegistViewController ()
@property (weak, nonatomic) IBOutlet UIButton *tickBtn;
@property (weak, nonatomic) IBOutlet UIButton *dagou;

@property (weak, nonatomic) IBOutlet UIButton *RegistBtn;

@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];    
    //设置title
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    [titleLabel setText:@"用户注册"];
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    [titleLabel setTextColor:[UIColor blackColor]];
    [titleLabel setFont:[UIFont boldSystemFontOfSize:19]];
    [titleLabel setBackgroundColor:[UIColor clearColor]];
    self.navigationItem.titleView = titleLabel;
    
    UIBarButtonItem * backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backUserBtn)];
    self.navigationItem.leftBarButtonItem = backButtonItem;
    self.RegistBtn.enabled = NO;
    self.RegistBtn.backgroundColor = [UIColor lightGrayColor];
    self.RegistBtn.layer.cornerRadius = 10;
}

- (void)backUserBtn {
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (IBAction)jumpProtocol:(id)sender {
   RegisterProtocolController *ProtocolVC = [[RegisterProtocolController alloc] init];
    UINavigationController *ProtocolNC = [[UINavigationController alloc] initWithRootViewController:ProtocolVC];
      ProtocolNC.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    
    [self presentViewController:ProtocolNC animated:YES completion:nil];
    
}

- (IBAction)tickBtnClicked:(id)sender {
    self.tickBtn.hidden = YES;
    self.dagou.hidden = NO;
    self.RegistBtn.enabled = YES;
    self.RegistBtn.backgroundColor = [UIColor orangeColor];
    
}

- (IBAction)dagouBtnClicked:(id)sender {
    self.tickBtn.hidden = NO;
    self.dagou.hidden = YES;
    self.RegistBtn.enabled = NO;
    self.RegistBtn.backgroundColor = [UIColor lightGrayColor];
  
}

- (IBAction)RegistBtnClicked:(id)sender {
    //注册时，用户名和密码不能为空
    if ([self.emailTextField.text isEqualToString:@""] || [self.pswTextField.text isEqualToString:@""]) {
        
        [self p_showAlertView:@"提示" message:@"用户名和密码不能为空"];
        return;
    }
    //昵称不能为空
    if ([self.nameTextField.text isEqualToString:@""]) {
        [self p_showAlertView:@"提示" message:@"昵称不能为空"];
        return;
    }
    //判定邮箱格式
    if (![self CheckInputEmail:self.emailTextField.text ]) {
        [self p_showAlertView:@"提示" message:@"请输入正确的邮箱格式"];
        return;
    }
    
    //判定密码位数
    if (![self CheckInputPassword:self.pswTextField.text]) {
        [self p_showAlertView:@"提示" message:@"密码必须在6-16位之间"];
        return;
    }
    
    
    BmobUser *bUser = [[BmobUser alloc]init];
    
    NSString *name = self.nameTextField.text;
    NSString *psw = self.pswTextField.text;
    NSString *email = self.emailTextField.text;
    
    [bUser setUserName:name];
    [bUser setPassword:psw];
    [bUser setEmail:email];
  
    [bUser signUpInBackgroundWithBlock:^ (BOOL isSuccessful, NSError *error){
        if (isSuccessful){
            NSLog(@"Sign up successfully");
            NSString *name =  self.nameTextField.text;
         
         
                    UserController *user = [[UserController alloc]init];
                    
                    
                    [self presentViewController:user animated:YES completion:nil];
                    user.logoutBtn.hidden = NO;
                    user.firstView.hidden = NO;
                    user.loginButton.hidden = YES;
                    user.firstLabel.hidden = YES;
                    user.logoutBtn.layer.cornerRadius = 10;
                    
                    // BmobUser *bUser = [BmobUser getCurrentObject];
                    user.nickTextField.text = name;
                    
            
        } else {
            NSLog(@"%@",error);
        }
    }];
    
    
}

- (void)p_showAlertView:(NSString *)title message:(NSString *)message
{
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
  
}

-(BOOL)CheckInputEmail:(NSString *)text
{
    NSString *Regex=@"[A-Z0-9a-z._%+-]+@[A-Z0-9a-z._]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",Regex];
    return [emailTest evaluateWithObject:text];
    
}
-(BOOL)CheckInputPassword:(NSString *)text
{
    NSString *Regex = @"\\w{6,16}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    return [emailTest evaluateWithObject:text];
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    
    [self.view endEditing:YES];
    
}









@end
